package svc

import (
	"github.com/hibiken/asynq"
	"github.com/zeromicro/go-zero/core/stores/redis"
	"go-zero-admin/application/job/test/internal/config"
	"go-zero-admin/pkg/orm"
	"go-zero-admin/pkg/utils"
)

type ServiceContext struct {
	Config      config.Config
	RedisClient *redis.Redis

	/* asynq 任务组件 */
	AsynqScheduler *asynq.Scheduler //调度器
	AsynqInspector *asynq.Inspector //检查器客户端
	AsynqServer    *asynq.Server    //服务端
	AsynqClient    *asynq.Client    //客户端
	DB             *orm.DB          //数据库
}

func NewServiceContext(c config.Config) *ServiceContext {
	conf := redis.RedisConf{
		Host: c.RedisConf.Host,
		Type: c.RedisConf.Type,
		Pass: c.RedisConf.Pass,
		Tls:  c.RedisConf.Tls,
	}
	db := orm.MustNewMysql(&orm.Config{
		DSN:          c.DB.DataSource,
		MaxOpenConns: c.DB.MaxOpenConns,
		MaxIdleCnns:  c.DB.MaxIdleConns,
		MaxLifetime:  c.DB.MaxLifetime,
	})
	return &ServiceContext{
		Config:         c,
		RedisClient:    redis.MustNewRedis(conf),
		AsynqScheduler: utils.NewScheduler(conf.Host, conf.Pass),
		AsynqInspector: utils.NewInspector(conf.Host, conf.Pass),
		AsynqServer:    utils.NewAsynqServer(conf.Host, conf.Pass),
		AsynqClient:    utils.NewAsynqClient(conf.Host, conf.Pass),
		DB:             db,
	}
}
